//控制层
app.controller('tiebaController', function ($scope, $controller, tiebaService) {

    $controller('baseController', {$scope: $scope});//继承

    //读取列表数据绑定到表单中
    $scope.findAll = function () {
        tiebaService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }

    //分页
    $scope.findPage = function (page, rows) {
        tiebaService.findPage(page, rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    //查询实体
    $scope.findOne = function () {
        var id = getUrlSearch('id');
        tiebaService.findOne(id).success(
            function (response) {
                $scope.entity = response.msg;
            }
        );
    }

    //查询实体
    $scope.updateOne = function (id) {
        tiebaService.updateOne(id).success(
            function (response) {
                $scope.entity = response.msg;
            }
        );
    }

    //保存
    $scope.save = function () {
        var serviceObject;//服务层对象
        if ($scope.entity.id != null) {//如果有ID
            serviceObject = tiebaService.update($scope.entity); //修改
        } else {
            serviceObject = tiebaService.add($scope.entity);//增加
        }
        serviceObject.success(
            function (response) {
                if (response.code == 1000) {
                    $scope.entity = {}
                    //重新查询
                    alert(response.msg);
                    $scope.reloadList();//重新加载
                    // $scope.search();//重新加载
                } else {
                    alert(response.msg);
                }
            }
        );
    }


    //批量删除
    $scope.dele = function () {
        //获取选中的复选框
        tiebaService.dele($scope.selectIds).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                    $scope.selectIds = [];
                }
            }
        );
    }

    $scope.searchEntity = {};//定义搜索对象

    //从cookie中搜索搜索
    $scope.search = function (page, rows) {
        var qq = getCookie('qq');
        $scope.searchEntity.qq = '';
        if (qq != null || qq != '') {
            $scope.searchEntity.qq = qq;
        }
        tiebaService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                $scope.list = response.msg.rows;
                // $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
    }

    //普通搜索
    $scope.searchKeyword = function (page, rows) {
        tiebaService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                $scope.list = response.msg.rows;
                // $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
    }

    //搜索Email
    $scope.searchEmail = function () {
        tiebaService.searchEmail($scope.searchEntity).success(
            function (response) {
                $scope.entity = response.msg;
                // $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
    }
    //添加邮箱
    $scope.addEmail = function () {
        tiebaService.addEmail($scope.entity).success(
            function (response) {
                var msg = response.msg;
                alert(msg);
                $scope.entity = {};
            }
        );
    }

    //搜索
    $scope.covert = function (validity) {
        if (validity == 1) {
            return '有效';
        } else {
            return '无效';
        }
    }

});
